package sy3;
import java.util.ArrayList;

public class Exercise {
    private ArrayList<BinaryOperation> operations;
    private int operationCount;
    
    /**
     * 默认构造方法
     */
    public Exercise() {
        this.operations = new ArrayList<>();
        this.operationCount = 0;
    }
    
    /**
     * 检查是否包含某个算式
     */
    public boolean contains(BinaryOperation op) {
        for (BinaryOperation operation : operations) {
            if (operation.equals(op)) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * 生成加法习题
     */
    public void generateAdditionExercise(int count) {
        operations.clear();
        operationCount = 0;
        
        while (operationCount < count) {
            BinaryOperation op = new BinaryOperation();
            op.generateAdditionOperation();
            
            if (!contains(op)) {
                operations.add(op);
                operationCount++;
            }
        }
    }
    
    /**
     * 生成减法习题
     */
    public void generateSubstractExercise(int count) {
        operations.clear();
        operationCount = 0;
        
        while (operationCount < count) {
            BinaryOperation op = new BinaryOperation();
            op.generateSubstractOperation();
            
            if (!contains(op)) {
                operations.add(op);
                operationCount++;
            }
        }
    }
    
    /**
     * 生成混合习题
     */
    public void generateBinaryExercise(int count) {
        operations.clear();
        operationCount = 0;
        
        while (operationCount < count) {
            BinaryOperation op = new BinaryOperation();
            op.generateBinaryOperation();
            
            if (!contains(op)) {
                operations.add(op);
                operationCount++;
            }
        }
    }
    
    /**
     * 格式化显示
     */
    public void formateAndDisplay(int columnsPerRow) {
        for (int i = 0; i < operationCount; i++) {
            System.out.printf("%2d. %-8s", i + 1, operations.get(i).toString());
            
            // 每行显示指定数量的题目后换行
            if ((i + 1) % columnsPerRow == 0) {
                System.out.println();
            }
        }
        
        // 如果最后一行不满，也要换行
        if (operationCount % columnsPerRow != 0) {
            System.out.println();
        }
    }
    
    // 获取题目数量
    public int getOperationCount() {
        return operationCount;
    }
}